package com.tanhua.mongo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.mongodto.RecommendUserDto;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.Random;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询今日佳人
     *
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser findRecommendUser(Long toUserId) {

        //根据toUserId查询，根据评分score排序，获取第一条
        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);

        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    /**
     * 获取当前用户推荐朋友
     *
     * @param page
     * @param pagesize
     * @param toUserId
     * @return
     */
    @Override
    public PageResult findRecommendFriend(Integer page, Integer pagesize, Long toUserId) {
        //根据toUserId查询，根据评分score排序
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //查询总数
        Query query = new Query(criteria);

        long count = mongoTemplate.count(query, RecommendUser.class);

        query.skip((page-1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("score")));

        List<RecommendUser> list = mongoTemplate.find(query, RecommendUser.class);

        return new PageResult(page,pagesize,Math.toIntExact(count),list);
    }

    /**
     * 查询佳人缘分值
     *
     * @param personalId
     * @param userId
     * @return
     */
    @Override
    public RecommendUser findByPersonalId(Long personalId, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(personalId).and("toUserId").is(userId));
        RecommendUser user = mongoTemplate.findOne(query, RecommendUser.class);
        //判断是否为空,如果为空则设置一个默认信息
        if (user == null){
            user = new RecommendUser();
            user.setUserId(personalId);
            user.setToUserId(userId);
            user.setScore(88d);
        }
        return user;
    }

    /**
     * 推荐用户列表
     *
     * @param userId
     * @param count
     * @return
     */
    @Override
    public List<RecommendUser> findCards(Long userId, int count) {
        //查询user_like表判断改用的喜欢与不喜欢
        List<UserLike> userLikeList = mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
        List<Long> likeUserIds = CollUtil.getFieldValues(userLikeList, "likeUserId",Long.class);
        //查询推荐用户表排除查询出的用户id
        Criteria criteria = Criteria.where("toUserId").is(userId)
                .and("userId").nin(likeUserIds);

        //3.使用统计函数，随机获取推荐的用户列表
        TypedAggregation aggregation = new TypedAggregation<>(
                RecommendUser.class,
                Aggregation.match(criteria), //指定查询条件
                Aggregation.sample(count)
        );
        AggregationResults<RecommendUser> aggregate = mongoTemplate.aggregate(aggregation, RecommendUser.class);

        return aggregate.getMappedResults();
    }
}
